﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Examples
{
    /// <summary>
    /// Connects to the Camelot WCF Service using Ntlm/Windows authentication
    /// </summary>
    class WcfServiceUsingNtlmAuthentication
    {
        public static void DoIt(string userName, string password, string domain)
        {
            using (var camelotService = new WcfService.CamelotServiceClient())
            {
                camelotService.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(userName, password, domain);
                camelotService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

                try
                {
                    var user = camelotService.GetUser();
                    Console.WriteLine("Current context user is: " + (user != null ? user : "Impersonation not enabled in Wcf Service (ENABLE_IMPERSONATION)"));
                }
                catch (System.ServiceModel.Security.MessageSecurityException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }
}
